package cqrtplm.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelImageUtils {
    
    /**
     * 从Excel中提取所有浮动图片，并按单元格位置分类
     * @param workbook Excel工作簿
     * @return 按"sheetName_row_col"格式组织的图片Map
     */
    public static Map<String, List<byte[]>> extractAllFloatingImages(Workbook workbook) {
        Map<String, List<byte[]>> imageMap = new HashMap<>();
        
        if (!(workbook instanceof XSSFWorkbook)) {
            return imageMap; // 仅支持XSSF格式
        }
        
        XSSFWorkbook xssfWorkbook = (XSSFWorkbook) workbook;
        
        for (int i = 0; i < xssfWorkbook.getNumberOfSheets(); i++) {
            XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
            
            if (sheet != null) {
                XSSFDrawing drawing = sheet.getDrawingPatriarch();
                
                if (drawing != null) {
                    for (XSSFShape shape : drawing.getShapes()) {
                        if (shape instanceof XSSFPicture) {
                            XSSFPicture picture = (XSSFPicture) shape;
                            XSSFClientAnchor anchor = (XSSFClientAnchor) picture.getAnchor();
                            
                            if (anchor != null) {
                                // 获取图片所在单元格位置
                                int row = anchor.getRow1();
                                int col = anchor.getCol1();
                                
                                // 生成唯一键
                                String key = sheet.getSheetName() + "_" + row + "_" + col;
                                
                                // 获取图片数据
                                PictureData pictureData = picture.getPictureData();
                                byte[] imageData = pictureData.getData();
                                
                                // 添加到Map
                                if (!imageMap.containsKey(key)) {
                                    imageMap.put(key, new ArrayList<>());
                                }
                                imageMap.get(key).add(imageData);
                            }
                        }
                    }
                }
            }
        }
        
        return imageMap;
    }
    
    /**
     * 获取指定单元格的图片数据
     * @param imageMap 图片Map
     * @param sheetName 工作表名称
     * @param row 行号
     * @param col 列号
     * @return 图片数据列表
     */
    public static List<byte[]> getImagesByCell(Map<String, List<byte[]>> imageMap, 
                                              String sheetName, int row, int col) {
        String key = sheetName + "_" + row + "_" + col;
        return imageMap.getOrDefault(key, new ArrayList<>());
    }
    
    /**
     * 将图片数据转换为Base64字符串
     * @param imageData 图片数据
     * @param format 图片格式
     * @return Base64编码的字符串
     */
    public static String convertToBase64(byte[] imageData, String format) {
        return java.util.Base64.getEncoder().encodeToString(imageData);
    }
    
    /**
     * 获取图片格式
     * @param pictureData 图片数据对象
     * @return 图片格式字符串
     */
    public static String getImageFormat(PictureData pictureData) {
        switch (pictureData.getPictureType()) {
            case Workbook.PICTURE_TYPE_JPEG:
                return "jpeg";
            case Workbook.PICTURE_TYPE_PNG:
                return "png";
            case Workbook.PICTURE_TYPE_EMF:
                return "emf";
            case Workbook.PICTURE_TYPE_WMF:
                return "wmf";
            case Workbook.PICTURE_TYPE_DIB:
                return "dib";
            default:
                return "unknown";
        }
    }
}